package dslab.util;

/**
 * container for abstract DMTP message, wraps the data with useful utilities
 */
public class Message {
    public String sender;
    public String recipients;
    public String subject;
    public String data;
    public String hash;

    /**
     * default constructor
     */
    public Message() {}

    /**
     * copy constructor
     * @param message Message object to copy
     */
    public Message(Message message) {
        this.sender = message.sender;
        this.recipients = message.recipients;
        this.subject = message.subject;
        this.data = message.data;
        this.hash = message.hash;
    }

    /**
     * fills the message with parts (like the lines of the 'show' directive)
     * @param part a line formatted according to '(from|to|subject|data|hash) content'
     */
    public void fill(String part) {
        // skip malformed parts
        if (!part.contains(" "))
            return;

        String[] headAndBody = part.split(" ", 2);
        switch (headAndBody[0]) {
            case "from":
                this.sender = headAndBody[1];
                break;
            case "to":
                this.recipients = headAndBody[1];
                break;
            case "subject":
                this.subject = headAndBody[1];
                break;
            case "data":
                this.data = headAndBody[1];
                break;
            case "hash":
                this.hash = headAndBody[1];
                break;
            default:
                // shouldn't appear
                break;
        }
    }

    /**
     * query completion of message
     * @return whether all fields are set
     */
    public boolean isMessageIncomplete() {
        return (recipients != null) && (sender != null) && (subject != null) && (data != null);
    }

    /**
     * gets a formatted string
     * @return formatted string containing a message that shows which fields are not set
     */
    public String getMissingFields() {
        StringBuilder stringBuilder = new StringBuilder("no");
        if (recipients == null)
            stringBuilder.append(" to");
        if (sender == null)
            stringBuilder.append(" from");
        if (subject == null)
            stringBuilder.append(" subject");
        if (data == null)
            stringBuilder.append(" data");
        return stringBuilder.toString();
    }

    /**
     * resets all fields to null
     */
    public void reset() {
        recipients = sender = subject = data = null;
    }

    /**
     * overridden toString implementation
     * @return string representation Message
     */
    @Override
    public String toString() {
        return String.format(
            "{ 'from': '%s', 'to': '%s', 'subject': '%s', 'data': '%s', 'hash': '%s' }",
            sender,
            recipients,
            subject,
            data,
            hash
        );
    }

    public String allFields() {
        return String.join("\n", sender, recipients, subject, data);
    }
}
